import requests
from bs4 import BeautifulSoup
import re


def crawl_news(url='https://news.baidu.com'):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    try:
        response = requests.get(url, headers=headers, timeout=10)
        soup = BeautifulSoup(response.text, 'html.parser')

        # 获取热点新闻
        hot_news = []
        for item in soup.select('.hotnews a'):
            title = item.get_text().strip()
            link = item['href']
            if title and link:
                hot_news.append({'title': title, 'link': link})

        return hot_news[:10]  # 返回前10条

    except Exception as e:
        print(f"爬取失败: {e}")
        return []